import CONFIG from "./constant.js"

function Core(){
	
}



/* ===============================number */	


/* ===============================string */


/* ===============================object */



/* ===============================datetime */

var getImgPathFromLocal=function(e){
	return "../../static/img/"+e;  //打包app时 从本地读取
}
var getImgPathFromLocalDeep3=function(e){
	return "../../../static/img/"+e;  //打包app时 从本地读取
}
var getImgPathFromOss=function(e){
	return "http://hwhypordstatic.oss-cn-hangzhou.aliyuncs.com/img/"+e;    //打包小程序时，删除本地的static 里的图片；从oss获取图片
	
}
Core.prototype.getImgPath=function(e){
	//return getImgPathFromLocal(e);  //打包app时 从本地读取
	return getImgPathFromOss(e)    //打包小程序时，删除本地的static 里的图片；从oss获取图片	
}
Core.prototype.getImgPathFromLocalDeep3=getImgPathFromLocalDeep3;
Core.prototype.getImgPathFromLocal=getImgPathFromLocal;
Core.prototype.getImgPathFromOss=getImgPathFromOss;


Core.prototype.getPlat=function(){
	let ret={};
	//ret.platform=uni.getSystemInfoSync().platform;
	
	// #ifdef H5 
	ret.plat="H5";
	// #endif 
	
	// #ifdef MP-WEIXIN 
	ret.plat="MP-WEIXIN";
	// #endif
	// #ifdef MP-ALIPAY
	ret.plat="MP-ALIPAY";
	// #endif
	// #ifdef MP-BAIDU
	ret.plat="MP-BAIDU";
	// #endif
	// #ifdef MP-TOUTIAO
	ret.plat="MP-TOUTIAO";
	// #endif
	// #ifdef MP-QQ
	ret.plat="MP-QQ";
	// #endif
	// #ifdef MP-360
	ret.plat="MP-360";
	// #endif
	
	// #ifdef APP-PLUS
	ret.plat="APP-PLUS";
	// #endif
	// #ifdef APP-PLUS-NVUE
	ret.plat="APP-PLUS-NVUE";
	// #endif 
		
	return ret.plat;
}

Core.prototype.isWeixin=function(){
	return (typeof navigator!="undefined" 
	&& typeof navigator.userAgent!="undefined"
	&& navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger"
	)
}

Core.prototype.isApp=function(){
	let b1= "APP-PLUS"==mainUtil.getPlat();
	let b2= "APP-PLUS-NVUE"==mainUtil.getPlat();
	return b1||b2;
};

Core.prototype.isH5=function(){
	return "H5"==mainUtil.getPlat();	
};

Core.prototype.getSysInfo=function(){
    let o=uni.getSystemInfoSync();	
	return o;
};

/* ===============================platinfo */



Core.prototype.getQuery = function(url){
	    if(!mainUtil.isH5())return {};
	
	    var obj = {};
		if(typeof url=="undefined")url= location.href;
		var query = url.substr(url.indexOf("?"));
		query=query.substr(1);
		var reg = /([^=&\s]+)[=\s]*([^=&\s]*)/g;
		while(reg.exec(query)){
			obj[RegExp.$1] = decodeURI(RegExp.$2);
		}
		
	    return obj;
}
Core.prototype.openImage=function(o){
	mainUtil.data_local("show_img_param",o);
	mainUtil.navigateTo("/pages/common/show-img")
}
Core.prototype.jump_to_exter_page=function(src,title){
	if(mainUtil.isWeixin()){
		location.href=src;
	}else{
		let o={};
		o.src=src;o.title=title;
		mainUtil.data_local("page_url",o)
		mainUtil.navigateTo("/pages/common/web-page");
	}		
}

Core.prototype.navigateTo=function (url) {  
   let _url=url;if(url.indexOf("/pages")==-1)_url="/pages/"+url;
	uni.navigateTo({
		"url":_url,
		"complete":function(res){
			if("navigateTo:fail can not navigateTo a tabbar page"==res.errMsg){
				mainUtil.switchTab(_url);
			}
		}
	})
}
Core.prototype.redirectTo=function (url) {  
   let _url=url;if(url.indexOf("/pages")==-1)_url="/pages/"+url;
	uni.redirectTo({
		"url":_url,
		"complete":function(res){
			if(res.errMsg.indexOf("fail")>-1){
				mainUtil.switchTab(_url);
			} 
		}
	})
}
Core.prototype.reLaunch=function (url) {  
   let _url=url;if(url.indexOf("/pages")==-1)_url="/pages/"+url;
	uni.reLaunch({
	    url: _url,
		"complete":function(res){
			if("navigateTo:fail can not navigateTo a tabbar page"==res.errMsg){
				mainUtil.switchTab(_url);
			}
		}
	});
}
Core.prototype.switchTab=function (url) {  
   let _url=url;if(url.indexOf("/pages")==-1)_url="/pages/"+url;
	uni.switchTab({
		"url":_url
	})
}

/* ===============================navigetr */

Core.prototype.data_session=function(k,d){
	//console.log("data",k,d)
	if(typeof d=="undefined"){
		var o = sessionStorage.getItem(k);
		if(o==null){
			return null;
		}else{
			o = JSON.parse(o)
			return o.data||o;
		}		
		
	}else if(null==d){
		return sessionStorage.removeItem(k)
	}else{
		var m = {};
		m.type = "string";
		m.data = d;
		sessionStorage.setItem(k, JSON.stringify(m));
	}
}

Core.prototype.data_local=function(k,d){
	if(typeof d=="undefined"){
		var o = uni.getStorageSync(k)
		//console.log(o==null||!o)
		if(o==null||!o){
			return null;
		}else{
			o = JSON.parse(o);
			return o;
		}						
	}else if(null==d){
		return uni.removeStorageSync(k)
	}else{
		return uni.setStorageSync(k,JSON.stringify(d))
	}
}

/* ===============================datachace */


Core.prototype.comfirm = (_content='content',func,_title='提示')=>{
	uni.showModal({
		title: _title,
		content: _content,
		success: function (res) {
			if(res.confirm==true){
				func();
			}		
		}
	});	
}
Core.prototype.toast = (title, duration=1500, mask=false, icon='none')=>{
	//统一提示方便全局修改
	if(Boolean(title) === false){
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

/* ===============================interactive */



/*=============================== xhr */





Core.prototype.getConst=function(){	
	return CONFIG;
}
Core.prototype.getConfig =function(){	
	let ret=mainUtil.data_local("server_config")||{};
	
	return ret;
}




/*=============================== other */




Core.prototype.getUser=function(){
	return mainUtil.data_local("user_info")||{};
};
Core.prototype.setUser=function(u){
	/* if(typeof u=="string"){
		u=strUtil.getMap(u);
	} */
	mainUtil.data_local("user_info",u);
};


Core.prototype.rmLoginMsg=function(){
	mainUtil.setUser(null);
	
	mainUtil.data_local("token",null);
	mainUtil.data_local("password",null);
	
	mainUtil.data_local("pre_guider_page",null);
	mainUtil.data_local("pre_reg_page",null);
	
};



/* ========================================================= */

var mainUtil=new Core();
export default mainUtil